package com.spoplar.assistant;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import redis.clients.jedis.Jedis;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class StriveDegreTimeServlet extends HttpServlet {

	private Jedis jedis;
	

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		jedis = new Jedis("127.0.0.1",6379);
		jedis.select(5);
	}
	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/json");
		PrintWriter out = response.getWriter();
		String route_id = request.getParameter("route_id");
		String degree = request.getParameter("degree");
		String ttime = request.getParameter("ttime");
		
		JSONArray tmp_route_jarr = new JSONArray();
		String didi_route_lists = jedis.get("didi_route_lists");
		JSONArray route_jarr = JSON.parseArray(didi_route_lists);
		for(Iterator iterator = route_jarr.iterator();iterator.hasNext();){
			 JSONObject obj =(JSONObject)iterator.next();
			 String t_route_id = obj.getString("route_id");
			 if(route_id.equals(t_route_id)){
				 obj.remove("strive_degree");
				 obj.put("strive_degree",degree);
				 obj.remove("strive_time");
				 obj.put("strive_time",ttime);
			 }
			 tmp_route_jarr.add(obj);
		}
		jedis.set("didi_route_lists", tmp_route_jarr.toJSONString());
		
		String didi_suspense_list = jedis.get("didi_suspense_list");
		JSONArray suspense_jarr = JSON.parseArray(didi_suspense_list);
		JSONArray tmp_suspense_jarr = new JSONArray();
		for(Iterator iterator = suspense_jarr.iterator();iterator.hasNext();){
			 JSONObject obj =(JSONObject)iterator.next();
			 obj.remove("is_strive");
			 String t_route_id = obj.getString("route_id");
			 if(route_id.equals(t_route_id)){
				 obj.remove("strive_degree");
				 obj.put("strive_degree",degree);
				 obj.remove("strive_time");
				 obj.put("strive_time",ttime);
			 }
			 tmp_suspense_jarr.add(obj);
		}
		jedis.set("didi_suspense_list", tmp_suspense_jarr.toJSONString());
		
		StringBuffer buff = new StringBuffer();
		buff.append("{");
		buff.append("'code'").append(":").append("'OK'").append(",");
		buff.append("'data'").append(":").append("");
		buff.append("}");
		out.println(buff.toString());
		
		out.flush();
		out.close();
		
	}
	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		if(jedis !=null){
			try{
				jedis.close();
				jedis = null;
			}catch(Exception $e){
				System.out.println($e.getMessage().toString());
			}finally{
				jedis.close();
				jedis = null;
			}
		}
	}
}
